<?php

namespace App\Models;

use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Models\Menu
 *
 * @property int $id
 * @property int $menu_type
 * @property int|null $menu_id
 * @property string $name
 * @property string|null $direct
 * @property int $order
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property string|null $deleted_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Menu[] $children
 * @property-read \App\Models\Menu $parent
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Menu filter($input = array(), $filter = null)
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Menu newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Menu newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Menu onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Menu paginateFilter($perPage = null, $columns = array(), $pageName = 'page', $page = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Menu query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Menu simplePaginateFilter($perPage = null, $columns = array(), $pageName = 'page', $page = null)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Menu whereBeginsWith($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Menu whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Menu whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Menu whereDirect($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Menu whereEndsWith($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Menu whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Menu whereLike($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Menu whereMenuId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Menu whereMenuType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Menu whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Menu whereOrder($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Menu whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Menu withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Menu withoutTrashed()
 * @mixin \Eloquent
 * @property int|null $status
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Menu whereStatus($value)
 */
class Menu extends Model
{
    use SoftDeletes, Filterable;

    protected $fillable = ['menu_type', 'menu_id', 'name', 'direct', 'order', 'status'];

    protected $casts = [
        'id' => 'int',
        'menu_type' => 'int',
        'menu_id' => 'int',
        'order' => 'int',
        'status' => 'int'
    ];

    /**
     * @var array
     */
    protected $hidden = [
        'created_at', 'updated_at', 'deleted_at'
    ];

    const TYPE_EMPTY = 0;
    const TYPE_TOP = 1;
    const TYPE_CATEGORY = 2;
    const TYPE_ARCHIVE = 3;
    const TYPE_SINGLE_PAGE = 4;
    const TYPE_LINK = 5;
    const TYPE_TEXT
        = [
            self::TYPE_EMPTY => '空菜单',
            self::TYPE_TOP => '一级菜单',
            self::TYPE_CATEGORY => '栏目菜单',
            self::TYPE_ARCHIVE => '归档菜单',
            self::TYPE_SINGLE_PAGE => '单页菜单',
            self::TYPE_LINK => '外链菜单',
        ];

    public function children()
    {
        return $this->hasMany(self::class);
    }

    public function parent()
    {
        return $this->belongsTo(self::class);
    }

    /**
     * 递归获取树形索引
     *
     * @param integer
     * @param integer
     *
     * @return array 角色数组
     */
    public function getTreeIndex($id = 0, $deep = 0)
    {
        static $tempArr = [];
        $data = $this->query()->where('menu_id', $id)->orderBy('order', 'asc')
            ->get();
        foreach ($data as $k => $v) {
            $v->deep = $deep;
            $v->name = str_repeat("&nbsp;&nbsp;", $v->deep * 2).'|--'.$v->name;
            $tempArr[] = $v;
            $this->getTreeIndex($v->id, $deep + 1);
        }
        return $tempArr;
    }
}